<?php

class Cms_ClipController extends Henry_Controller_Action
{
	private $allLang;
    public function init()
    {
        parent::init();
        $this->allLang = Henry_Languages::getAllLang();
    }

    public function indexAction()
    {
    	$this->view->allLangJson = Zend_Json::encode($this->allLang);
    }
	public function updateAction()
    {
    	$catOb = new Henry_ClipCategories_Categories();
    	$id = $this->_request->getParam("id",-1);
    	
    	$currentLang = $this->_request->getParam('lg',Henry_Global::CMS_DEFAULT_LANG);
    	if (!in_array($currentLang, array_keys($this->allLang))) {
    		throw new Zend_Controller_Action_Exception("Khong Ton Tai",Henry_Error::NOT_EXISTS_LANG);
    	}
    	
    	if ($this->_request->isPost()){
    		$data = $this->_request->getParams();
    		if ($data['id']>0){
    			$sucess = $catOb->updateCategories($data, $data['id']);
    		}else{
    			$sucess = $catOb->addCategories($data);	
    		}
    	}
    	if($id != -1){ //Lay recode hien tao
			$item = $catOb->find($id)->toArray();
			$this->view->currentItem = $item[0];
    	}
    	if (isset($sucess)){
    		$this->sucess = $sucess;
    	}
    	$this->view->catTree = $catOb->getCatTree($currentLang);
    	$this->view->defaultCMSLANG = $currentLang;
    }
    public function testAction()
    {
		$this->_helper->viewRenderer->setNoRender(true);
		$ct = new Henry_Contents();
		$data = $ct->checkExists('footer','vi');
		Henry_TestUtil::printNoDie($data);
    }
    
	public function menuAction()
	{
		$thongtinchungMenu = array(
//			array(
//				'title' => 'Phân Loại Media',
//				'link'  => array(
//					'module'    =>'cms',
//					'controller'=>'clip',
//					'action'    =>'index'
//				)
//			),
			array(
				'title' => 'Cập Nhập Media',
				'link'  => array(
					'module'    =>'cms',
					'controller'=>'clip',
					'action'    =>'medialist',
				)
			)
		);
		$this->view->menu = $thongtinchungMenu;
	}
	
	//Lay du lieu mau
	public function getsampledataAction(){
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$filename = $this->_request->getParam('filename','null.html');
		$filePath = WEB_BASE_URL.'/sampledata/product/categories/'.$filename.".html";
		$content = '';
		if (file_exists($filePath))
			$content = file_get_contents($filePath);
		echo $content;	
	}
	
	public function postDispatch(){
		$this->view->allLang = $this->allLang;
        $tmp = $this->view->getScriptPaths();
        $jsPath = str_replace($_SERVER['DOCUMENT_ROOT'], "", $tmp[0]).$this->_request->getControllerName()."/js";
        $jsPath = str_replace("\\", "/", $jsPath);
        $this->view->jsPath = $jsPath;
        $this->view->itemInPage = 20;
        $catOb = new Henry_ClipCategories_Categories();
        $this->view->totalItem = $catOb->getTotalRecord();
	} 
	
	//---------------------------------------------
	//Product Ajax Process
	public function getcatlistAction(){
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		$SortColumsArr = array(
			0=>'categories_title',//default Cloum Sort
			3=>'categories_id',
			4=>'categories_title',
			5=>'parent_cat_title',
			6=>'created',
			7=>'language'
		);
		
		
		$currentLang = $this->_request->getParam("language",Henry_Global::CMS_DEFAULT_LANG);
		$sSortDir_0 = $this->_request->getParam("sSortDir_0","asc");
		$iSortCol_0 = $this->_request->getParam("iSortCol_0",0);
		$iDisplayStart = $this->_request->getParam("iDisplayStart",null);
		$iDisplayLength = $this->_request->getParam("iDisplayLength",null); 
		$catOb = new Henry_ClipCategories_Categories();
		$iTotal  = $this->_request->getParam('totalItem',$catOb->getTotalRecord($currentLang));
		$data = $catOb->getCatelist($currentLang,array('categories_id','categories_title','created','categories_p0','categories_p1','categories_p2','categories_p3','language','parent_id'),null,$SortColumsArr[$iSortCol_0]." ".$sSortDir_0,$iDisplayLength,$iDisplayStart);
		$output = array(
			"sEcho" => intval($_GET['sEcho']),
			"iTotalRecords" => $iTotal,
			"iTotalDisplayRecords" => count($data),
			"aaData" => $data
		);
		echo Zend_Json::encode($output); 		
	}
	public function readAction(){
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$callback = $this->_request->getParam("callback");
		$clipOb = new Henry_ClipCategories_Categories();
		$start = $this->_request->getParam("start");
		$sort = $this->_request->getParam("sort",'categories_id');
		$limit = $this->_request->getParam("limit");
		$dir = $this->_request->getParam("dir",'ASC');
		/*
		 * /$_GET['start'],$_GET['limit'],(isset($_GET['sort']))?$_GET['sort']:'id',(isset($_GET['dir']))?$_GET['dir']:'ASC'
		 */
	    // Create the output object.
	    $result = array(
	    	'success'=>true,
	    	'message'=>'Loaded data',
	    	'start'=>$this->_request->getParam('start'),
	    	'total'=>$clipOb->getTotal(),
	    	'data'=>$clipOb->getCatListExt($sort, $start, $limit, $dir)
	    );
		$jsonOutPut = Zend_Json::encode($result, Zend_Json::TYPE_ARRAY);
		//start output
		if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}
		
	}
	
	public function deleteAction(){
		$id = $this->_request->getParam("id");
		$clipOb = new Henry_ClipCategories_Categories();
		$clipOb->delRecode($id);
		$this->_redirect("/cms/clip");
	}
	
	public function medialistAction(){
		
	}
	
	public function updatemediaAction(){
		$catOb = new Henry_ClipCategories_Categories();
		$clipOb = new Henry_Clip_Clip();
    	$id = $this->_request->getParam("id",-1);
    	
    	$currentLang = $this->_request->getParam('lg',Henry_Global::CMS_DEFAULT_LANG);
    	if (!in_array($currentLang, array_keys($this->allLang))) {
    		throw new Zend_Controller_Action_Exception("Khong Ton Tai",Henry_Error::NOT_EXISTS_LANG);
    	}
    	
    	if ($this->_request->isPost()){
    		$data = $this->_request->getParams();
                if (empty ($data['clip_showtype'])){
                    $data['clip_showtype'] = "";
                }
    		if ($data['id']>0){
    			$sucess = $clipOb->updateCategories($data, $data['id']);
    		}else{
    			$sucess = $clipOb->addCategories($data);	
    		}
    	}
    	if($id != -1){ //Lay recode hien tao
			$item = $clipOb->find($id)->toArray();
			$this->view->currentItem = $item[0];
    	}
    	if (isset($sucess)){
    		$this->sucess = $sucess;
                $this->_redirect("/".$this->_request->getModuleName()."/".$this->_request->getControllerName()."/medialist");
    	}
    	$this->view->catTree = $catOb->getCatTree($currentLang);
    	$this->view->defaultCMSLANG = $currentLang;
	}
	public function readmediaAction(){
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$callback = $this->_request->getParam("callback");
		$clipOb = new Henry_Clip_Clip();
		$start = $this->_request->getParam("start");
		$sort = $this->_request->getParam("sort",'clip_id');
		$limit = $this->_request->getParam("limit");
		$dir = $this->_request->getParam("dir",'ASC');
		/*
		 * /$_GET['start'],$_GET['limit'],(isset($_GET['sort']))?$_GET['sort']:'id',(isset($_GET['dir']))?$_GET['dir']:'ASC'
		 */
	    // Create the output object.
	    $result = array(
	    	'success'=>true,
	    	'message'=>'Loaded data',
	    	'start'=>$this->_request->getParam('start'),
	    	'total'=>$clipOb->getTotal(),
	    	'data'=>$clipOb->getCatListExt($sort, $start, $limit, $dir)
	    );
		$jsonOutPut = Zend_Json::encode($result, Zend_Json::TYPE_ARRAY);
		//start output
		if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}
	}
	
	public function deletemediaAction(){
		$id = $this->_request->getParam("id");
		$clipOb = new Henry_Clip_Clip();
		$clipOb->delRecode($id);
		$this->_redirect("/cms/clip/medialist");
	}
}

